package com.wangsd.system.controller;

import com.wangsd.common.controller.UploadFile;
import com.wangsd.common.jwt.PassToken;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.common.utils.OssFileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "通用请求处理")
@RestController
@RequestMapping("/system/common")
@Slf4j
public class CommonController {

    @PassToken
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Object uploadFile(@ApiParam(value = "文件", required = true) MultipartFile file) {
        UploadFile uploadFile = new UploadFile();
        String fileName = DateUtils.getCurDateTimeSSS() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        try {
            String fileUrl = OssFileUtils.uploadFile(fileName, file.getInputStream(), "jinke/");
            uploadFile.setFilename(fileName);
            uploadFile.setFileUrl(fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        uploadFile.setCode(200);
        return uploadFile;
    }

}
